<template>
  <div class="app-container">
    <div id="map"></div>
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="所属区域" prop="regionId">
        <el-select
          clearable
          v-model="queryParams.regionId"
          @change="searchRegionChange"
        >
          <el-option
            v-for="(item, index) of regionOption"
            :key="index"
            :label="item.label"
            :value="item.value"
            >{{ item.label }}
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属门店" prop="storeId">
        <el-select clearable v-model="queryParams.storeId">
          <el-option
            v-for="(item, index) of searchStoreOption"
            :key="index"
            :label="item.label"
            :value="item.value"
            >{{ item.label }}
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="下单时间">
        <el-date-picker
          v-model="time"
          style="width: 240px"
          :picker-options="pickerOptions"
          value-format="yyyy-MM-dd"
          type="daterange"
          @change="getTime"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
              >新增</el-button>
            </el-col>  -->
      <!-- <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="single"
                @click="handleUpdate"
              >修改</el-button>
            </el-col>-->
      <el-col :span="1.5">
        <el-button plain size="mini" v-hasPermi="['business:additionalStatics:export']" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="storeList" max-height="650px">
      <!-- <el-table-column label="编号" align="center" prop="id" /> -->
      <!-- <el-table-column label="适用区域" align="center" prop="name" /> -->
      <el-table-column label="归属门店" align="center" prop="storeName" />
      <el-table-column
        label="附加商品消费总额"
        align="center"
        prop="additionalProductTotalConsumption"
      />
      <el-table-column
        label="附加商品消费数量"
        align="center"
        prop="additionalProductQuantity"
      />
      <el-table-column
        label="储值卡支付金额"
        align="center"
        prop="rechargeBalanceDeductionAmount"
      />
      <!-- <el-table-column
        label="赠送余额抵扣金额"
        align="center"
        prop="giftBalanceDeductionAmount"
      /> -->
      <el-table-column
        label="小程序微信支付金额"
        align="center"
        prop="miniProgramPaymentAmount"
      />
      <el-table-column
        label="银联扫码支付金额"
        align="center"
        prop="unionPayScanCodePaymentAmount"
      />
      <el-table-column
        label="现金支付金额"
        align="center"
        prop="cashPaymentAmount"
      />
      <el-table-column label="提成总额" align="center" prop="commissionTotal" />
      <el-table-column label="成本总额" align="center" prop="costTotal" />
      <el-table-column label="门店收益" align="center" prop="storeEarnings" >
           <template slot-scope="scope">
              {{ Number(scope.row.additionalProductTotalConsumption)-Number(scope.row.costTotal)}}
           </template>
      </el-table-column>
         
      <el-table-column
        label="项目订单购买百分比"
        align="center"
        prop="projectOrderPurchasePercentage"
      >
          <template slot-scope="scope">
              {{ Number(scope.row.itemBillCount)/(Number(scope.row.additionalProductQuantity)==0?1:Number(scope.row.additionalProductQuantity))*100+'%'}}
           </template>

      </el-table-column>

      <!-- <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="250"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleDetails(scope.row)"
            >查看详情</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listcoupon } from "@/api/system/coupon/coupondetailed";
import { salesReport } from "@/api/statistics";

import { fetchStoreList, fetchRegionList } from "@/api/system/orderList/order";

export default {
  name: "Store",
  data() {
    return {
      map: null,
      local: null,
      mk: null,
      latitude: "",
      longitude: "",
      keyWords: "",
      result: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 门店表格数据
      storeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        regionId: null,
        storeId: null,
        startTime: null,
        endTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      options: [],

      region: [],
      store: [],
      time: [],

      type: [],
      project: [],
      regionOption: [],
      searchStoreOption: [],
      pickerOptions: {
        shortcuts: [
          {
            text: "当日",
            onClick(picker) {
              picker.$emit("pick", [new Date(), new Date()]);
            },
          },
          {
            text: "昨日",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 2);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "当月",
            onClick(picker) {
              const end = new Date();
              const start = new Date(end.getFullYear(), end.getMonth(), 1);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "上月",
            onClick(picker) {
             // const end = new Date();
             const start = new Date();
              // start.setMonth(start.getMonth() - 1); // 设置开始时间为上月同一天
              // start.setDate(end.getDate()); // 确保开始时间和结束时间在同一天
              start.setMonth(start.getMonth() - 1, 1); // 设置开始时间为上月1号
              const end = new Date(start.getFullYear(), start.getMonth() + 1, 0); // 获取上月的最后一天
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getRegionOption();
  },
  methods: {
    getTime() {
      if (this.time.length) {
        this.queryParams.startTime = this.time[0];
        this.queryParams.endTime = this.time[1];
      } else {
        this.queryParams.startTime = null;
        this.queryParams.endTime = null;
      }
    },
    getRegionOption() {
      fetchRegionList().then((res) => {
        this.regionOption = res.data.map((rItem) => {
          return {
            value: rItem.id,
            label: rItem.regionName,
          };
        });
      });
    },

    searchRegionChange(value) {
      if (value) {
        this.queryParams.storeId = "";
      }
      fetchStoreList({ regionId: value }).then((res) => {
        this.searchStoreOption = res.data.map((rItem) => {
          return {
            label: rItem.storeName,
            value: rItem.id,
          };
        });
      });
    },

    getList() {
      this.loading = true;
      salesReport(this.queryParams).then((response) => {
        this.storeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        storeName: null,
        phoneNumber: null,
        province: null,
        provinceString: null,
        city: null,
        cityString: null,
        county: null,
        countyString: null,
        detail: null,
        longitude: null,
        latitude: null,
        intro: null,
        images: null,
        status: null,
        isInitiate: null,
        openingTime: null,
        closingTime: null,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.time = [];
      this.getTime();
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleDetails() {
      this.$router.push("/statistics/additionalStaticsDetails");
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "statistics/salesReportExport",
        {
          ...this.queryParams,
        },
        `store_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
